package com.ibook.controller;

import com.github.pagehelper.PageInfo;
import com.ibook.bo.BookBo;
import com.ibook.common.Const;
import com.ibook.common.ServerResponse;
import com.ibook.pojo.Categories;
import com.ibook.pojo.Tag;
import com.ibook.pojo.User;
import com.ibook.service.ProductService;
import com.ibook.vo.BookVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by heziqi on 2017/6/5.
 */
@Controller
@RequestMapping("/product/")
public class ProductController {

    @Autowired
    private ProductService productService;

    /**
     * 首页显示书籍信息：按图书大类分别显示热度排行top10的书籍
     */
    @RequestMapping("bookList.do")
    @ResponseBody
    public ServerResponse<PageInfo> bookList(int categoriesId,
                                             @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                             @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
        return productService.bookList(categoriesId,pageNum,pageSize);
    }

    /**
     * 书籍信息模糊查询：图书名，图书类别，作者，出版社等
     */
    @RequestMapping("searchList.do")
    @ResponseBody
    public ServerResponse<PageInfo> searchList(@RequestParam(value = "bookName",required = false)String bookName,
                                               @RequestParam(value = "categoriesName",required = false)String categoriesName,
                                               @RequestParam(value = "authorName",required = false)String authorName,
                                               @RequestParam(value = "publishingName",required = false)String publishingName,
                                             @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                             @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
        return productService.searchLsit(bookName,categoriesName,authorName,publishingName,pageNum,pageSize);
    }

    /**
     * 查询所有图书大类别
     */
    @RequestMapping("cateList.do")
    @ResponseBody
    public ServerResponse<List<Categories>> cateList(){
        return productService.cateList();
    }

    /**
     * 查询大类下面的热门标签
     */
    @RequestMapping("tagList.do")
    @ResponseBody
    public ServerResponse<List<Tag>> tagList(int categoriesId){
        return productService.tagList(categoriesId);
    }

    /**
     * 书籍详情
     */
    @RequestMapping("bookDetail.do")
    @ResponseBody
    public ServerResponse<BookBo> bookDetail(int bookId){
        return productService.bookDetail(bookId);
    }

    /**
     * 侧边栏推荐相关书籍
     */
    @RequestMapping("bookRecom.do")
    @ResponseBody
    public ServerResponse<List<BookVo>> bookRecom(int categoriesId){
        return productService.bookRecom(categoriesId);
    }

    /**
     * 增加书籍热度
     */
    @RequestMapping("raiseHots.do")
    @ResponseBody
    public ServerResponse raiseHots(HttpSession httpSession,int bookId){
        User user = (User)httpSession.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorMessage("用户未登录");
        }
        return productService.raiseHots(bookId);
    }

    /**
     *评论书籍
     */
    @RequestMapping("commentToBook.do")
    @ResponseBody
    public ServerResponse commentToBook(HttpSession httpSession,int bookId,String detail){
        User user = (User)httpSession.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorMessage("用户未登录");
        }
        return productService.commentToBook(user.getUserId(),bookId,detail);
    }

    /**
     * 显示书的评论
     */
    @RequestMapping("bookComments.do")
    @ResponseBody
    public ServerResponse<PageInfo> bookComments(int bookId,
                                                 @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                                 @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
        return productService.bookComments(bookId,pageNum,pageSize);
    }
}
